package com.kws.tool.controller;

import cn.hutool.json.JSONUtil;
import com.kws.common.core.domain.R;
import com.kws.system.api.domain.QuerySignUrlRq;
import com.kws.tool.dto.*;
import com.kws.tool.dto.info.*;
import com.kws.tool.service.ElectronicSignatureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <b>ElectronicSignatureController</b>
 *
 * @description: ElectronicSignatureController <br>
 * @date: 2024/11/11 09:46 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "电子签章")
@RestController
public class ElectronicSignatureController {
    private static final Logger logger = LoggerFactory.getLogger(ElectronicSignatureController.class);

    @Autowired
    private ElectronicSignatureService electronicSignatureService;

    @ApiOperation("获取电子签章文件上传地址")
    @PostMapping("/get/fileUploadUrl")
    public R<QueryFileUploadUrlInfo> getFileUploadUrl(@RequestBody QueryFileUploadRq rq) throws Exception {
        logger.info("获取e签宝电子签章文件上传地址:{}"+ JSONUtil.parse(rq));
        QueryFileUploadUrlInfo info = electronicSignatureService.getFileUploadUrl(rq);
        return R.ok(info);
    }

    @ApiOperation("查询文件上传状态")
    @GetMapping("/query/fileUpload/status")
    public R<QueryFileUploadStatusInfo> queryFileUploadStatus(@RequestParam String fileId) throws Exception {
        QueryFileUploadStatusInfo info = electronicSignatureService.queryFileUploadStatus(fileId);
        return R.ok(info);
    }


    @ApiOperation("获取制作合同模板页面")
    @PostMapping("/query/templateMakingUrl")
    public R<QueryTemplateMakingInfo> queryTemplateMaking(@RequestBody QueryTemplateMakingRq rq) throws Exception {
        QueryTemplateMakingInfo info =  electronicSignatureService.queryTemplateMaking(rq);
        return R.ok(info);
    }

//    @ApiOperation("创建控件组")
//    @PostMapping("/create/controlGroup")
//    public R createControlGroup(@RequestBody CreateControlGroupRq rq) throws Exception {
//        List<String> componentGroupIdList = electronicSignatureService.createControlGroup(rq);
//        return R.ok(componentGroupIdList);
//    }

    @ApiOperation("查询合同模板中控件详情")
    @GetMapping("/query/templateControl")
    public R<QueryTemplateControlInfo> queryTemplateControl(@RequestParam String docTemplateId) throws Exception {
        QueryTemplateControlInfo info = electronicSignatureService.queryTemplateControl(docTemplateId);
        return R.ok(info);
    }

    @ApiOperation("获取填写合同模板页面")
    @PostMapping("/query/templateFillIn")
    public R<QueryTemplateFillInUrlInfo> queryTemplateFillInUrl(@RequestBody QueryTemplateFillInUrlRq rq) throws Exception {
        QueryTemplateFillInUrlInfo info = electronicSignatureService.queryTemplateFillInUrl(rq);
        return R.ok(info);
    }

    @ApiOperation("基于文件发起签署")
    @PostMapping("/query/signContract")
    public R<QuerySignContractInfo> querySignContract(@RequestBody QuerySignContractRq rq) throws Exception {
        QuerySignContractInfo info = electronicSignatureService.querySignContract(rq);
        return R.ok(info);
    }

    @ApiOperation("获取签署页面链接")
    @PostMapping("/query/signPage")
    public R<QuerySignPageInfo> querySignPageUrl(@RequestBody QuerySignPageRq rq) throws Exception {
        QuerySignPageInfo info = electronicSignatureService.querySignPageUrl(rq);
        return R.ok(info);
    }

    @ApiOperation("获取签署连接")
    @PostMapping("/get/signUrl")
    public R getSignUrl(@RequestBody QuerySignUrlRq rq) throws Exception {
        QuerySignUrlInfo info = electronicSignatureService.getSignUrl(rq);
        return R.ok(info);
    }

    @ApiOperation("查询签署流程详情")
    @GetMapping("/query/signingProcess")
    public R<QuerySigningProcessInfo> querySigningProcess(@RequestParam String signFlowId) throws Exception {
        QuerySigningProcessInfo info = electronicSignatureService.querySigningProcess(signFlowId);
        return R.ok(info);
    }

    @ApiOperation("完结签署流程")
    @GetMapping("/completed/signingProcess")
    public R completedSigningPProcess(@RequestParam String signFlowId) throws Exception {
        boolean status = electronicSignatureService.completedSigningPProcess(signFlowId);
        return R.ok(status);
    }

    @ApiOperation("下载已签署文件及附属材料")
    @GetMapping("/download/signContract")
    public R<DownloadSignContractInfo> downloadSignContract(@RequestParam String signFlowId) throws Exception {
        DownloadSignContractInfo info = electronicSignatureService.downloadSignContract(signFlowId);
        return R.ok(info);
    }

}
